home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 May / PCPlus May 1998=disk A.iso / full / CBUILDER / SAMS / SAMPLES / CHAP09 / SPMAIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-12  |  5.7 KB  |  192 lines

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4.  
  5. #include "SPMain.h"
  6. #include "SPAbout.h"
  7. //---------------------------------------------------------------------------
  8. #pragma resource "*.dfm"
  9. TScratchPad *ScratchPad;
  10. //---------------------------------------------------------------------------
  11. __fastcall TScratchPad::TScratchPad(TComponent* Owner)
  12.   : TForm(Owner)
  13. {
  14. }
  15. //---------------------------------------------------------------------------
  16. void __fastcall TScratchPad::FileNewClick(TObject *Sender)
  17. {
  18.     //
  19.     // Open a file. First check to see if the current file
  20.   // needs to be saved.
  21.   //
  22.     if (Memo->Modified) {
  23.     //
  24.     // Display a message box.
  25.     //
  26.       int result = Application->MessageBox(
  27.         "The current file has changed. Save changes?",
  28.       "ScratchPad Message", MB_YESNOCANCEL | MB_ICONWARNING);
  29.     //
  30.     // If Yes was clicked then save the currnt file.
  31.     //
  32.        if (result == IDYES) FileSaveClick(Sender);
  33.     //
  34.     // If No was clicked then do nothing.
  35.     //
  36.     if (result == IDCANCEL) return;
  37.   }
  38.   //
  39.   // Delete the strings in the memo, if any.
  40.   //
  41.   if (Memo->Lines->Count > 0) Memo->Clear();
  42.   //
  43.   // Set the FileName property of the Save Dialog to a
  44.   // blank string. This lets us know that the file has
  45.   // not yet been saved.
  46.   //
  47.   SaveDialog->FileName = "";
  48. }
  49. //---------------------------------------------------------------------
  50. void __fastcall TScratchPad::FileOpenClick(TObject *Sender)
  51. {
  52.     //
  53.     // Open a file. First check to see if the current file needs
  54.   // to be saved. Same logic as in FileNewClick() above.
  55.   //
  56.     if (Memo->Modified) {
  57.       int result = Application->MessageBox(
  58.         "The current file has changed. Save changes?",
  59.       "ScratchPad Message", MB_YESNOCANCEL | MB_ICONWARNING);
  60.        if (result == IDYES) FileSaveClick(0);
  61.     if (result == IDCANCEL) return;
  62.   }
  63.   //
  64.   // Execute the File Open dialog. If OK was pressed then
  65.   // open the file using the LoadFromFile() method. First
  66.   // clear the FileName property.
  67.   //
  68.   OpenDialog->FileName = "";
  69.   if (OpenDialog->Execute())
  70.     {
  71.       if (Memo->Lines->Count > 0) Memo->Clear();
  72.          Memo->Lines->LoadFromFile(OpenDialog->FileName);
  73.     SaveDialog->FileName = OpenDialog->FileName;
  74.   }
  75. }
  76. //---------------------------------------------------------------------
  77. void __fastcall TScratchPad::FileSaveClick(TObject *Sender)
  78. {
  79.     //
  80.   // If a filename has already been provided then there is
  81.   // no need to bring up the File Save dialog. Just save the
  82.   // file using SaveToFile().
  83.   //
  84.     if (SaveDialog->FileName != "")
  85.   {
  86.       Memo->Lines->SaveToFile(SaveDialog->FileName);
  87.     //
  88.     // Set Modified to false since we've just saved.
  89.     //
  90.     Memo->Modified = false;
  91.   }
  92.   //
  93.   // If no filename was set then do a SaveAs().
  94.   //
  95.     else FileSaveAsClick(Sender);
  96. }
  97. //---------------------------------------------------------------------
  98. void __fastcall TScratchPad::FileSaveAsClick(TObject *Sender)
  99. {
  100.   //
  101.   // Display the File Save dialog to save the file.
  102.   // Set Modified to false since we just saved.
  103.   //
  104.     SaveDialog->Title = "Save As";
  105.     if (SaveDialog->Execute())
  106.   {
  107.       Memo->Lines->SaveToFile(SaveDialog->FileName);
  108.     Memo->Modified = false;
  109.   }
  110. }
  111. //---------------------------------------------------------------------
  112. void __fastcall TScratchPad::FileExitClick(TObject *Sender)
  113. {
  114.     //
  115.   // All done. Close the form.
  116.   //
  117.     Close();
  118. }
  119. //---------------------------------------------------------------------
  120. void __fastcall TScratchPad::EditUndoClick(TObject *Sender)
  121. {
  122.     //
  123.   // TMemo doesn't have an Undo method so we have to send
  124.   // a Windows WM_UNDO message to the memo component.
  125.   //
  126.     SendMessage(Memo->Handle, WM_UNDO, 0, 0);
  127. }
  128. //---------------------------------------------------------------------
  129. void __fastcall TScratchPad::EditSelectAllClick(TObject *Sender)
  130. {
  131.     //
  132.   // Just call TMemo::SelectAll().
  133.   //
  134.     Memo->SelectAll();
  135. }
  136. //---------------------------------------------------------------------
  137. void __fastcall TScratchPad::EditCutClick(TObject *Sender)
  138. {
  139.     //
  140.   // Call TMemo::CutToClipboard().
  141.   //
  142.     Memo->CutToClipboard();
  143. }
  144. //---------------------------------------------------------------------
  145. void __fastcall TScratchPad::EditCopyClick(TObject *Sender)
  146. {
  147.     //
  148.   // Call TMemo::CopyToClipboard().
  149.   //
  150.     Memo->CopyToClipboard();
  151. }
  152. //---------------------------------------------------------------------
  153. void __fastcall TScratchPad::EditPasteClick(TObject *Sender)
  154. {
  155.     //
  156.   // Call TMemo::PasteFromClipboard().
  157.   //
  158.     Memo->PasteFromClipboard();
  159. }
  160. //---------------------------------------------------------------------
  161. void __fastcall TScratchPad::EditWordWrapClick(TObject *Sender)
  162. {
  163.     //
  164.   // Toggle the TMemo::WordWrap property. Set the Checked
  165.   // property of the menu item to the same value as WordWrap.
  166.   //
  167.     Memo->WordWrap = !Memo->WordWrap;
  168.   EditWordWrap->Checked = Memo->WordWrap;
  169.   //
  170.   // If WordWrap is on then we only need the vertical scroll
  171.   // bar. If it's off, then we need both scroll bars.
  172.   //
  173.   if (Memo->WordWrap) Memo->ScrollBars = ssVertical;
  174.   else Memo->ScrollBars = ssBoth;
  175. }
  176. //---------------------------------------------------------------------
  177.  
  178. void __fastcall TScratchPad::HelpAboutClick(TObject *Sender)
  179. {
  180.   AboutBox->ShowModal();  
  181. }
  182. //---------------------------------------------------------------------
  183. void __fastcall TScratchPad::FormCreate(TObject *Sender)
  184. {
  185.   Application->OnHint = &OnHint;
  186. }
  187. //---------------------------------------------------------------------
  188. void __fastcall TScratchPad::OnHint(TObject* Sender)
  189. {
  190.   StatusBar->SimpleText = Application->Hint;
  191. }
  192.